package com.sdna.unified;

import java.io.UnsupportedEncodingException;

import com.sdna.rpc.RPCClientMgr;
import com.sdna.rpc.RPCClient;
import com.sdna.rpc.ParameterBuilder;
import java.util.HashMap;


/**
 * provide service for group including add a new group for a sdid, change the
 * information of a particular group, delete a particular group to get all the
 * group of a person(sdid).
 * 
 * @author yangjiandong
 * @since 2010-2-25
 * @file GroupService.java
 */

/**
  * This file is generated by GenerateServiceClient.py, SVN location:
  *     svn://192.168.90.69/i.sdo.com/sdk/trunk/unified/GenerateServiceClient.py
  *
  * Please DO NOT change this file!!! it is just a wrapper of unified service api.   
  */

public class GroupServiceClient {

	/**
	 * get a group object according to the groupid
	 * 
	 * @param groupId
	 * @return group
	 */
	static public String getGroup(Long groupId, Long sdid) throws UnsupportedEncodingException{
		ParameterBuilder pb = new ParameterBuilder();
		pb.add("groupId", groupId);
		pb.add("sdid", sdid);
		RPCClient client = RPCClientMgr.getClient();
		try {
			return client.doRequest("GroupService.getGroup", pb.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			RPCClientMgr.restore(client);
		}
	}

	/**
	 * list all the people in a specific group
	 * 
	 * @param sdid
	 *            sdid and groupId should match each other
	 * @param groupId
	 * @param startIndex
	 * @param count
	 * @return the relationFollow list
	 * @throws ServiceInvocationException
	 */
	static public String getPeopleFromGroup(Long sdid, Long groupId, HashMap<String, Object> parameters) throws UnsupportedEncodingException{
		ParameterBuilder pb = new ParameterBuilder();
		pb.add("sdid", sdid);
		pb.add("groupId", groupId);
		if (parameters.containsKey("startIndex")) pb.add("startIndex", (Integer)parameters.get("startIndex"));
		if (parameters.containsKey("count")) pb.add("count", (Integer)parameters.get("count"));
		RPCClient client = RPCClientMgr.getClient();
		try {
			return client.doRequest("GroupService.getPeopleFromGroup", pb.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			RPCClientMgr.restore(client);
		}
	}

	/**
	 * get one person's grouplist,
	 * 
	 * @param sdid
	 * @return the list of groups the sdid owns
	 */
	static public String getPersonGroups(Long sdid) throws UnsupportedEncodingException{
		ParameterBuilder pb = new ParameterBuilder();
		pb.add("sdid", sdid);
		RPCClient client = RPCClientMgr.getClient();
		try {
			return client.doRequest("GroupService.getPersonGroups", pb.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			RPCClientMgr.restore(client);
		}
	}

	/**
	 * gernerating a group object
	 * 
	 * @param sdid
	 * @param groupName
	 * @return
	 * @throws ServiceInvocationException
	 *             when the value in the input group was not valid
	 */
	static public String addGroup(Long sdid, Long appId, String groupName) throws UnsupportedEncodingException{
		ParameterBuilder pb = new ParameterBuilder();
		pb.add("sdid", sdid);
		pb.add("appId", appId);
		pb.add("groupName", groupName);
		RPCClient client = RPCClientMgr.getClient();
		try {
			return client.doRequest("GroupService.addGroup", pb.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			RPCClientMgr.restore(client);
		}
	}

	/**
	 * modify a exsiting group, name for example
	 * 
	 * @param group
	 * @param name
	 * @throws InvalidValueException
	 */
	static public String updateGroupName(Long groupId, Long sdid, String groupName) throws UnsupportedEncodingException{
		ParameterBuilder pb = new ParameterBuilder();
		pb.add("groupId", groupId);
		pb.add("sdid", sdid);
		pb.add("groupName", groupName);
		RPCClient client = RPCClientMgr.getClient();
		try {
			return client.doRequest("GroupService.updateGroupName", pb.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			RPCClientMgr.restore(client);
		}
	}

	/**
	 * change the delete_tag of the group object to 1
	 * 
	 * @param gourpId
	 * @throws InvalidOperationException
	 *             when the group do not exist
	 */
	static public String deleteGroup(Long groupId, Long sdid) throws UnsupportedEncodingException{
		ParameterBuilder pb = new ParameterBuilder();
		pb.add("groupId", groupId);
		pb.add("sdid", sdid);
		RPCClient client = RPCClientMgr.getClient();
		try {
			return client.doRequest("GroupService.deleteGroup", pb.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			RPCClientMgr.restore(client);
		}
	}
}